package com.ying.tests.CCCbasis.Test3Socket;

import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.*;

@Slf4j
public class Test3NetworkClient {

    public static void main(String[] args) {
//        test1();                // URL处理
        args = new String[]{"localhost", "8888"};
        test2(args);            // Socket客户端实例

    }

    // URL处理
    private static void test1() {
        try {
//            URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se");
            URL url = new URL("http://www.runoob.com");
            System.out.println("URL为：" + url.toString());
            System.out.println("协议为：" + url.getProtocol());
            System.out.println("主机名为：" + url.getHost());
            System.out.println("端口为：" + url.getPort());
            System.out.println("默认端口为：" + url.getDefaultPort());
            System.out.println("路径为：" + url.getPort());
            System.out.println("文件名及请求参数：" + url.getFile());
            System.out.println("请求参数为：" + url.getQuery());
            System.out.println("定位位置：" + url.getRef());


            // 打开连接
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpURLConnection = null;
            if (urlConnection instanceof HttpURLConnection) {
                httpURLConnection = (HttpURLConnection) urlConnection;
            } else {
                System.out.println("请输入URL地址：");
                return;
            }

            // 反序列化
            BufferedReader in =
                    new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

            String urlString = "";
            String current;
            while (null != (current = in.readLine())) {
                urlString += current;
            }

            System.out.println(urlString);
        } catch (IOException e) {
            log.error("出错了！", e);
        }

    }

    // Socket客户端实例
    private static void test2(String[] args) {
        System.out.println("进入客户端");
        String serverName = args[0];
        int port = Integer.parseInt(args[1]);
        System.out.println("指定服务端主机名：" + serverName + " 指定服务端端口号：" + port);
        try {
            // 创建流套接字（socket）并将其连接到指定主机名上的指定端口号
            Socket client = new Socket(serverName, port);
            System.out.println("本机端口号：" + client.getLocalPort() + "...");
            System.out.println("远程服务端地址：" + client.getRemoteSocketAddress());
            OutputStream outToServer = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outToServer);

            // 发送的消息
            out.writeUTF("客户端发送的消息：Hello from " + client.getLocalSocketAddress());
            InputStream inFromServer = client.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);
            System.out.println("服务端响应的消息：" + in.readUTF());
            client.close();
        } catch (IOException e) {
            log.error("出错了！", e);
        }

    }
}

